package com.kun.controller.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kun.controller.BaseController;
import com.kun.entity.Comment;
import com.kun.result.ExceptionMsg;
import com.kun.result.Response;
import com.kun.service.BookService;
import com.kun.service.CommentService;
import com.kun.service.UserService;

/**
 * @description: 
 * @author: KM
 * 2018年5月9日 下午4:40:41
 */
@Controller
@RequiresAuthentication
@RequestMapping("/admin")
public class CommentAdminController extends BaseController{
	
	@Autowired
	private CommentService commmentService;
	
	@Autowired
	private BookService  bookServiceImpl;
	
	@Autowired
	private UserService  userServiceImpl;
	
	@RequiresPermissions("bookComment:view")
	@RequestMapping("/bookCommentList")
	public String bookCommentList(){
		return "admin/bookCommentList";
	}
	
	@RequiresPermissions("activityComment:view")
	@RequestMapping("/activityCommentList")
	public String activityCommentList(){
		return "admin/activityCommentList";
	}
	
	@RequiresPermissions("wishInfo:view")
	@RequestMapping("/wishList")
	public String wishList(){
		return "admin/wishCommentList";
	}
	
	//评论列表
	@RequestMapping("/getComment")
	@ResponseBody
	public Map<String,Object> getComment(@RequestParam(name = "begin") int begin,
			@RequestParam(name = "length") int length,@RequestParam(name = "commentType") String commentType,
			String jsonString,String nickname,String bookname){
		List<String> idList = new ArrayList<String>();
		List<Integer> bookidList = new ArrayList<Integer>();
		//nickname模糊查询出List<userid>
		idList	= userServiceImpl.selectUserByLikeNickname(nickname);
		//bookname 模糊查询出List<bookid>
		bookidList = bookServiceImpl.selectBookLikeBookName(bookname,"");
		Map<String,Object> map = commmentService.selectAllComment(begin, length, commentType, jsonString,idList,bookidList);
		Map<String,Object> m = new HashMap<String,Object>();
		m.put("count",map.get("allRecord"));
		m.put("data",map.get("commentList"));
		m.put("code", 0);
		m.put("msg", "");
		return m;
	}
	
	
	/**
	 * 获取许愿墙发起评论
	 * */
	@RequestMapping("/getWish")
	@ResponseBody
	public Map<String,Object> getWish(@RequestParam(name="begin") int begin,@RequestParam(name="length") int length,String jsonString){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap = commmentService.selectAllWish(begin, length, jsonString, null);
		Map<String,Object> m = new HashMap<String,Object>();
		m.put("count",resultMap.get("allRecord"));
		m.put("data",resultMap.get("wishList"));
		m.put("code", 0);
		m.put("msg", "");
		return m;
	}
	
	//修改评论
	@RequiresPermissions("commentInfo:update")
	@RequestMapping("/updateComment")
	@ResponseBody
	public Response updateComment(Comment comment){
		int i = commmentService.updateCommentByPrimaryKeySelect(comment);
		if(i>0){
			return result(ExceptionMsg.SUCCESS);
		}
		return result(ExceptionMsg.FAILED);
	}
	
	//删除评论
	@RequiresPermissions("commentInfo:delete")
	@RequestMapping("/deleteComment")
	@ResponseBody
	public Response deleteComment(@RequestParam("commentId") int commentId,Integer fatherId){
		int i = commmentService.deleteCommentByAdmin(commentId,fatherId);
		if(i>0){
				return result(ExceptionMsg.SUCCESS);
		}
		return result(ExceptionMsg.FAILED);
	}

}
